package logic

import (
	"3kz/models/mysql"
	"3kz/zk3rpc/internal/svc"
	"3kz/zk3rpc/zk3rpc"
	"context"
	"fmt"
	"gorm.io/gorm"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *zk3rpc.UpdateRequest) (*zk3rpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Model(&user).Where("id = ?", in.Id).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("账户不存在,请重新输入")
	}

	produce := mysql.Produce{
		Model:        gorm.Model{ID: uint(in.Id)},
		ProductCode:  in.ProductCode,
		ProductName:  in.ProductName,
		ProductPrice: in.ProductPrice,
		ProductCost:  in.ProductCost,
		Img:          in.Img,
	}
	mysql.DB.Debug().Model(&produce).Updates(&produce)

	return &zk3rpc.UpdateResponse{Id: int64(produce.ID)}, nil
}
